/*
 * Copyright 2004-2005 asoft
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.asoft.cn/licenses
 */

package com.asoft.examonline.exammanager.manager;

import java.util.List;

import org.apache.log4j.Logger;

import com.asoft.common.base.dao.DAO;
import com.asoft.common.base.manager.BaseManager;
import com.asoft.common.util.random.RandomArray;
import com.asoft.examonline.exammanager.dao.ExamQuestionDAO;
import com.asoft.examonline.exammanager.model.ExamQuestion;

/**
 * <p>Title: 试题</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: asoft</p>
 * @ $Author: author $
 * @ $Date: 2005/03/14 11:32:01 $
 * @ $Revision: 1.5 $
 * @ created in 2007-2-20
 *
 */

public class ExamQuestionManagerimp extends BaseManager
                                 implements ExamQuestionManager

{
        static Logger logger = Logger.getLogger(ExamQuestionManagerimp.class);

        /** 试题DAO */
        private ExamQuestionDAO examQuestionDAO;

        public void setExamQuestionDAO(ExamQuestionDAO examQuestionDAO)
        {
                // 设置BaseManager.dao
                super.setDAO(examQuestionDAO);
                this.examQuestionDAO = examQuestionDAO;
        }
        
        /**
         * 单元测试用 
         */
        @Override
        public void setDAO(DAO dao)
        {
                this.setExamQuestionDAO((ExamQuestionDAO)dao); 
        }
        
        public Object update(Object obj)
        {
                ExamQuestion examQuestion = (ExamQuestion)obj;
                // 删除老答案
                Object[] oldResults = examQuestion.getOldResults().toArray();
                for(int i = 0; i < oldResults.length;i ++)
                {
                        this.examQuestionDAO.remove(oldResults[i]);
                }
                return this.examQuestionDAO.update(examQuestion);
        }

        /**
         * 描述:查询试题s
         *
         * @ param:
         * @ param:orderBy 排序属性（for hql）
         * @ param:sortType 排序方式（asc or desc）
         * @ Exception:
         * @ return model集合
         */
        public List findExamQuestions(String domainId,
                                      String subjectId,
                                      String courseId,
                                      String unitId,
                                      String sectionId,
                                      String knowledgePointId,
                                      String knowledgeTypeId,
                                      String questTypeId,
                                      String difficultyId,
                                      String sourceId,
                                      String authorName,
                                      String content,
                                      String orderBy,
                                      String sortType)
        {
                return this.examQuestionDAO.findExamQuestions(domainId,
                                                              subjectId,
                                                              courseId,
                                                              unitId,
                                                              sectionId,
                                                              knowledgePointId,
                                                              knowledgeTypeId,
                                                              questTypeId,
                                                              difficultyId,
                                                              sourceId,
                                                              authorName,
                                                              content,
                                                              orderBy,
                                                              sortType);
        }
        
        /**
         * 描述:查询试题sBy知识点
         *
         * @ Exception:
         * @ return model集合
         */
        public List findExamQuestionsByKnowledgePoint(String knowledgePointId)
        {
                return this.examQuestionDAO.findExamQuestionsByKnowledgePoint(knowledgePointId);
        }
        
        /**
         * 描述:查询试题By知识点
         *
         * @ Exception:
         * @ return model集合
         */
        public ExamQuestion getRandomExamQuestionByKnowledgePoint(String knowledgePointId)
        {
                List eqs = this.examQuestionDAO.findExamQuestionsByKnowledgePoint(knowledgePointId);
                try
                {
                        int[] is = RandomArray.parse(eqs.size());
                        return (ExamQuestion)eqs.get(is[0]);
                        
                }
                catch(UnsupportedOperationException uoe)
                {
                        logger.error("获取随机试题失败:",uoe);
                        return null;
                }
        }
        
        
}
